options compress=YES;
libname manasi ''; 
libname f831 ''; 

* PARENT DEATH FROM NUMIDENT;

* USE ONLY HUN AND PAN FROM PARENTS' SSR RECORDS;
data manasi.parents_all_hunpanonly;
	set manasi.ssr_parents_full(keep=hun pan);
	if pan='*00000000' then delete;
run;

* ELIMINATE DUPLICATES BY HUN AND PAN AND SORT ;
proc sort data=manasi.parents_all_hunpanonly out=manasi.parents_all_hunpanonly nodupkey;
	by pan hun;
run;

* SORT NUMIDENT RECORDS ;
proc sort data=manasi.numident out=manasi.numident;
	by ssn;
run;

* MERGE IN PARENT NUMIDENT RECORDS ;
data manasi.par_numi_hunmerge;
	merge 	
			manasi.parents_all_hunpanonly 
			(rename=(pan=ssn) in=fromssrx)

			manasi.numident 
			(rename=(dod=pardod) in=fromnumx);

	by ssn; 
	fromssr = fromssrx;
	fromnum = fromnumx;

run;

proc tabulate data=manasi.par_numi_hunmerge;
	class fromnum fromssr;
	table fromnum, fromssr;
run;

* KEEP ONLY THOSE IN SSR;
data manasi.par_numi_hunmerge;
	set manasi.par_numi_hunmerge(where=(fromssr=1));
	rename fromnum=pardeath;
run;

* MERGE IN VARIABLE INDICATING WHETHER PARENT WAS IN HOUSEHOLD AT ANY TIME
BETWEEN 2002 AND 2006 (GENERAL TREATMENT PERIOD);
proc sort data=manasi.par_numi_hunmerge out=manasi.par_numi_hunmerge;
	by hun ssn;
run;

data manasi.par_numi_hunmerge;
	merge 	
			manasi.par_numi_hunmerge 

			manasi.ssr_parents_estpdonly_wide
			(rename=(pan=ssn) keep=hun pan inhh0206);

	by hun ssn; 
run;

proc sort data=manasi.par_numi_hunmerge out=manasi.par_numi_hunmerge;
	by hun;
run;

data manasi.par_numi_hunmerge_wide;
	set manasi.par_numi_hunmerge;
	par_id + 1;
	by hun;
	if first.hun then par_id=1;
run;

data manasi.par_numi_hunmerge_wide;
	set manasi.par_numi_hunmerge_wide;
	if ssn='*00000000' then delete;
run;

proc means data=manasi.par_numi_hunmerge_wide noprint;
	by hun;
	var par_id;
	output out=manasi.par_ssr_parid max(par_id)=max_parid;
run;

proc means mean median min max p90 p95 p99 data=manasi.par_ssr_parid;
	var max_parid;
run;

* COLLAPSE TO HUN LEVEL TO GET ONE RECORD FOR EACH KID;
data manasi.par_numi_hunmerge_wide;
	set manasi.par_numi_hunmerge_wide;
	by hun;

	keep 	hun 
			ssn1-ssn7 
			pardod1-pardod7
			pardeath1-pardeath7 
			inhh0206_1-inhh0206_7
			;	

	retain 	 
			ssn1-ssn7 
			pardod1-pardod7
			pardeath1-pardeath7 
			inhh0206_1-inhh0206_7
			;

	array	assn(1:7) 			ssn1-ssn7 ; 
	array	apardod(1:7) $ 8	pardod1-pardod7 ; 
	array	apardeath(1:7) 		pardeath1-pardeath7 ; 
	array 	ainhh0206(1:7)		inhh0206_1-inhh0206_7;	

	if first.hun then
	do;
		do i=1 to 7;
			assn(i) 		=.;	
			apardod(i)		="";
			apardeath(i)	=.;
			ainhh0206(i)		=.;
		end;
	end;

	assn(par_id) 		= ssn ;	
	apardod(par_id)		= pardod;
	apardeath(par_id)	= pardeath;
	ainhh0206(par_id)	= inhh0206;

	if last.hun then output;

run;

* SUM NUMBER OF PARENT DEATHS;
data manasi.par_numi_hunmerge_wide;
	set manasi.par_numi_hunmerge_wide;

	pardeathX0206_1=pardeath1*inhh0206_1;
	pardeathX0206_2=pardeath2*inhh0206_2;
	pardeathX0206_3=pardeath3*inhh0206_3;
	pardeathX0206_4=pardeath4*inhh0206_4;
	pardeathX0206_5=pardeath5*inhh0206_5;
	pardeathX0206_6=pardeath6*inhh0206_6;
	pardeathX0206_7=pardeath7*inhh0206_7;

	pardeathtot=sum(of pardeath1-pardeath7);
	pardeathtot0206=sum(of pardeathX0206:);

	drop ssn1-ssn7 pardeath1-pardeath7 pardeathX0206_1-pardeathX0206_7;
run;
